# -*-coding:utf-8 -*-
#author: LeoKim

import os
import hashlib
import pymysql
import configparser


import up_diff

# 加载现有配置文件
conf = configparser.ConfigParser()
conf.read('config.ini')


#连接数据库
conn = pymysql.connect(
    host = conf.get('config', 'host'),
    user = conf.get('config', 'user'),
    password = conf.get('config', 'password'),
    database = conf.get('config', 'database'),
    charset = conf.get('config', 'charset'),
    cursorclass = pymysql.cursors.DictCursor
    )

#大文件的MD5值
def File_md5(filename):
    _FILE_SLIM=100*1024*1024

    calltimes = 0     #分片的个数
    hmd5 = hashlib.md5()
    fp = open(filename, "rb")
    f_size = os.stat(filename).st_size #得到文件的大小
    if f_size > _FILE_SLIM:
        while (f_size > _FILE_SLIM):
            hmd5.update(fp.read(_FILE_SLIM))
            f_size /= _FILE_SLIM
            calltimes += 1  # delete    #文件大于100M时进行分片处理
        if (f_size > 0) and (f_size <= _FILE_SLIM):
            hmd5.update(fp.read())
    else:
        hmd5.update(fp.read())
    # return (hmd5.hexdigest(), calltimes)
    return hmd5.hexdigest()

def listDir(dirs):
    cursor = conn.cursor()
    for dir in dirs:
        for parent, dirnames, filenames in os.walk(dir):
            for filename in filenames:
                #文件路径
                file_full_path = os.path.join(parent, filename)
                file_path = file_full_path.replace(dir, "")
                file_md5_name = hashlib.md5(file_path.encode(encoding='UTF-8')).hexdigest()

                sql = "select count(*) as cnt from files where `md5` = '" + file_md5_name + "'"
                cursor.execute(sql)
                results = cursor.fetchall()

                if(results[0]['cnt']<1):
                    cursor.executemany("INSERT INTO files(md5,path) VALUES(%s,%s);", [(file_md5_name, file_path)])
                    conn.commit()

    print('listDir Done.')
    cursor.close()

def diff_dir(dirs):
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM files");
    row = cursor.fetchone()
    while row is not None:
        md5_f1=md5_f2=''
        f1_size=f2_size=''

        for dir in dirs:
            #get floder
            full_path = dir+row['path']
            path_split = full_path.split('\\')
            floder=path_split[3]


            if dir == dir_1:
                if os.path.exists(full_path):
                    f1_size=os.path.getsize(full_path)
                    md5_f1 = File_md5(full_path)
            if dir == dir_2:
                if os.path.exists(full_path):
                    f2_size=os.path.getsize(full_path)
                    md5_f2 = File_md5(full_path)

        # up_diff.update_files(floder, md5_f1, f1_size, md5_f2, f2_size, row['md5'])
        up_diff.update_files(floder, md5_f1, f1_size, md5_f2, f2_size, row['md5'])
        row = cursor.fetchone()
    print('Diff Done.')
    cursor.close()


dir_1 = conf.get('config', 'dir_1')
dir_2 = conf.get('config', 'dir_2')
# dir_3 = conf.get('config', 'dir_3')
dirs = [dir_1, dir_2]

#遍历所有文件
listDir(dirs)
#对比文件入库
diff_dir(dirs)

conn.close()


